package com.hippo.core.ui.compose.setting

import android.widget.Toast
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.hippo.core.R
import com.hippo.core.ui.components.BottomSheetDialog
import com.hippo.core.ui.components.InputEdit
import com.hippo.core.ui.components.ThemeButton


@Composable
fun ChangeEmailDialog(oldEmail:String,onDismissRequest: () -> Unit,onNext:(String) -> Unit) {

    BottomSheetDialog(
        title = stringResource(R.string.setting_31),
        onDismissRequest = onDismissRequest
    ) {
        DialogContent(oldEmail,onNext)

    }
}


@Composable
private fun DialogContent(oldEmail:String,onNext:(String) -> Unit) {

    val email = remember { mutableStateOf("") }

    val context = LocalContext.current

    Column {
        Spacer(modifier = Modifier.height(10.dp))

        InputEdit(
            modifier = Modifier.fillMaxWidth(),
            enable = false,
            text = oldEmail,
            hint = stringResource(R.string.login_2),
            onValueChange = {
                email.value = it
            },
            leftPainter = painterResource(R.mipmap.mail)
        )

        Spacer(modifier = Modifier.height(20.dp))

        InputEdit(
            modifier = Modifier.fillMaxWidth(),
            text = email.value,
            hint = stringResource(R.string.setting_32),
            onValueChange = {
                email.value = it
            },
            leftPainter = painterResource(R.mipmap.mail),
            keyboardOptions = KeyboardOptions(
                keyboardType = KeyboardType.Email
            )
        )

        Spacer(modifier = Modifier.height(20.dp))


        Text(
            text = stringResource(R.string.setting_33),
            modifier = Modifier.fillMaxWidth()
        )

        Spacer(modifier = Modifier.height(30.dp))

        ThemeButton(
            text = stringResource(R.string.next),
            modifier = Modifier.fillMaxWidth(),
            onClick = {
                if(email.value.isEmpty()){
                    Toast.makeText(context,context.getString(R.string.login_2),Toast.LENGTH_SHORT).show()
                    return@ThemeButton
                }
                onNext(email.value)
            }
        )
        Spacer(modifier = Modifier.height(30.dp))
    }
}



@Preview(showBackground = true)
@Composable
private fun ChangeEmailDialogPreview() {
    DialogContent(oldEmail = "sss@qq.com",  onNext = {})
}